// #ifdef XT_PC
import { Notification } from 'element-ui';
// #endif

export let notify = {
  exceptionConfig: {
    show: true,
    modal: false,
  },
  handleException(exception, {
    show=notify.exceptionConfig.show,
    modal=notify.exceptionConfig.modal,
  }={}) {
    if(!show) return
    let msg = exception?(typeof exception==='string'?exception:(exception.message||exception.errMsg)):''
    if(msg==='request:fail') msg = '网络请求失败'
    return modal?notify.showModal(msg):notify.showToast(msg)
  },

  showToast(options) {
    // this && this.$refs && this.$refs.uToast && this.$refs.uToast.show({
    // 	title: typeof options === 'string' ? options : '',
    // 	type: 'default',
    // 	position: 'bottom',
    // 	...(typeof options === 'string' ? {} : options)
    // })

    // #ifdef XT_PC
    Notification({
      // type: 'error',
      message: typeof options === 'string' ? options : '',
      ...(typeof options === 'string' ? {} : options)
    })
    return new Promise(resolve=>{
      setTimeout(()=>resolve(), 4500)
    })
    // #endif

    // #ifndef XT_PC
    uni.showToast({
      title: typeof options === 'string' ? options : '',
      icon: "none",
      ...(typeof options === 'string' ? {} : options)
    })
    return new Promise(resolve=>{
      setTimeout(()=>resolve(), 1800)
    })
    // #endif
  },
  
  showLoading(options) {
    uni.showLoading({
      title: typeof options === 'string' ? options : '加载中...',
      ...(typeof options === 'string' ? {} : options)
    })
  },
  
  hideLoading() {
    uni.hideLoading()
  },
  
  /**
   * @example
   * this.$x.notify.showModal({content:'xxx', showCancel: false})
   * this.$x.notify.showModal('xxx')
   * uni.$x.notify.showModal({title: 'xxx', content: 'default', editable: true, placeholderText: '请输入'}).then(v=>console.log(1111, v))
   */
  showModal(options) {
    options = (typeof options === 'string' ? {content: options} : options)
    return uni.showModal({
      content: options.content,
      cancelColor: uni.$x.theme.uniTextColorGrey,
      confirmColor: uni.$x.theme.uniColorPrimary,
      ...options
    }).then(([err,res])=>{
      if(err||res.cancel) return Promise.reject()
      return res.content
    })
  },
  showActionSheet(items, title='') {
    return uni.showActionSheet({
      title,
      itemList: items,
    }).then(([err, res])=>{
      if(err) return Promise.reject(err)
      return res.tapIndex
    })
  }
}


export default function($x, {
	notifyExtend
}={}, app) {
	Object.assign(notify, notifyExtend||{})
	$x.notify = notify
}
